<?php

class Rating extends database_object{
    
    public $product_id;
    public $company_id;
    public $user_id;
    public $rate;
  
    
    protected static $table_name = "bo_rating";
    protected static $db_fields = array("product_id", "company_id", "rate", "user_id");
    
    
    
     public static function getProductRating($id){
        $id = (int)$id; 
        $sql = "SELECT * FROM bo_rating where product_id ={$id}";
        return self::find_by_sql($sql);
        
    }
    
    public static function getCompanyRating($id){
        $id = (int)$id;
        $sql = "SELECT * FROM bo_rating where company_id ={$id}";
        return self::find_by_sql($sql);
        
    }
    
    
    public static function rateCompany($rating){
        
        $rating->save();
    }
    
    public static function rateProduct($rating){
        
        $rating->save();
    }
    
    public static function getAverageRate($id){
        
        $sql = "SELECT AVG( rate )
                FROM bo_rating, bo_user
                WHERE product_id ={$id}
                AND user_id = bo_user.id
                AND bo_user.sex = 'male'";
                
        $sql = "SELECT AVG( rate )
                FROM bo_rating, bo_user
                WHERE product_id ={$id}
                AND user_id = bo_user.id
                AND bo_user.sex = 'female'";
        
        $totalrate[] = self::executeSQL($sql);
        $totalrate[] = self::executeSQL($sql2);
        return $totalrate;
        
        
    }
    
    public static function getRateCount($id){
        
        $sql = "SELECT COUNT(id) FROM bo_rating WHERE product_id = {$id}";
        
        $count = self::executeSQL($sql);
        return $count;
    }
    }
?>

